package com.sto.stoapp.activity.scanner.inquiry;

import android.content.Context;
import android.database.Cursor;

import com.sto.stoapp.db.StoDataBase;
import com.sto.stoapp.db.manager.CastManager;
import com.sto.stoapp.entity.FlightCompareEntity;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.util.UnifiedStaticType;

import java.util.List;

public class InquiryFlightCompare {

    private Context context;
    private List<FlightCompareEntity> flightEntityLists;

    public InquiryFlightCompare(Context context1) {
        context = context1;
    }

    /**
     * 获得航班对比记录集
     * 
     * @return 
     */
    public List<FlightCompareEntity> getFlightCompareRecords(
            InquiryFieldSql fieldSql) {
        try {
            String date = fieldSql.getInquiryField()[0];// 获取日期
            flightEntityLists = CastManager.getInstance(context)
                    .queryFlightAndCountByDate(date);
            System.out.println("航班对比记录数量： " + flightEntityLists.size());
            if (flightEntityLists != null && flightEntityLists.size() > 0) {
                for (FlightCompareEntity entity : flightEntityLists) {
                    entity.setRealityCount(queryRealCount(new String[] { date,
                            entity.getFlight() }));
                }
            } 
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionLog.writeLog("statisticsDataStatus", e.getMessage());
        }
        return flightEntityLists;
    }

    /**
     * 查询实到数量
     */
    private String queryRealCount(String[] args) {
        String sql = "select count(*) " + " from " + " tab_airt_tdb "
                + " where " + " scan_date " + " like ? " + " and "
                + " next_station " + " = ? ";
        String realCount = null;
        StoDataBase stoDataBase = new StoDataBase(context);
        Cursor c = stoDataBase.getCursor(UnifiedStaticType.READ_DATABASE, sql,
                args);
        if (c != null && c.moveToFirst()) {
            realCount = c.getInt(0) + "";
        }
        stoDataBase.closeCursor(c);
        stoDataBase.closeDb(UnifiedStaticType.READ_DATABASE);
        return realCount;
    }
}
